﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using AutoMapper;
using HandyControl.Controls;
using HandyControl.Data;
using Prism.Commands;
using Prism.Services.Dialogs;
using YJTTool.Common.Controls;
using YJTTool.Common.Enums;
using YJTTool.Common.Events;
using YJTTool.Common.Helper;
using YJTTool.Common.Managers;
using YJTTool.Configure.Controls;
using YJTTool.Configure.Views;
using YJTTool.Core;
using YJTTool.Core.Global;
using YJTTool.Core.Utilities;
using YJTTool.DBBaseServer.AbstractModel;
using YJTTool.DBBaseServer.Entities;

namespace YJTTool.Configure.ViewModels
{
    public class ScheduledRemindersViewModel : InheritBase
    {
        #region 内部字段
        private readonly IDialogService _dialogService;
        private readonly AbstractTimerTaskServer abstractTimerTaskServer;
        private readonly IMapper mapper;
        #endregion

        #region 命令
        public DelegateCommand<TimerTask> DeleteCommand { get; private set; }
        public DelegateCommand CreateTaskCommand { get; private set; }
        #endregion

        #region ctor
        public ScheduledRemindersViewModel(IDialogService dialogService, AbstractTimerTaskServer abstractTimerTaskServer, IMapper mapper)
        {
            _dialogService = dialogService;
            this.mapper = mapper;
            this.abstractTimerTaskServer = abstractTimerTaskServer;
            TimerTask = new TimerTask();
            CreateTaskCommand = new DelegateCommand(OpenEditDialog);
            DeleteCommand = new DelegateCommand<TimerTask>(Delete);
            Query();
            GlobalMethodEvent.Instance.Subscribe(ReceiveMethodEvent);
        }
        #endregion

        #region 方法
        private void ReceiveMethodEvent(GlobalMethodEventParameter parameter)
        {
            if (parameter.MethodName == GlobalMethodNames.ScheduledRemindersStatusChanges)
            {
                switch (parameter.SourceModule)
                {
                    case "StatusChanges":
                        StatusChanges();
                        break;
                }
            }
        }
        private async void StatusChanges()
        {
            var data = TimerTask.DtotoDb(TimerTask);
            await abstractTimerTaskServer.UpdateAsync(data);
            MessageSuccess("更新成功!");
            await LoadingMaskInterceptor.Instance().InterceptAsync(async () => await Task.Run(() => { Query(); }));
        }
        private async void Delete(TimerTask task) 
        {
            var data = task.DtotoDb(task);
            var count = await abstractTimerTaskServer.DeleteByWhereAsync(s => s.Id == task.Id);
            if (count > 0)
            {
                MessageSuccess("删除成功!");
                await LoadingMaskInterceptor.Instance().InterceptAsync(async () => await Task.Run(() => { Query(); }));
            }
            else
            {
                MessageError("删除失败!");
            }
        }
        private void OpenEditDialog()
        {
            var parameters = new DialogParameters
           {
               { "IsEnableds", "" },  // 传递的数据
               { "EditedData", "" },  // 传递的数据
               
           };
            _dialogService.ShowDialog(nameof(CreateScheduledTaskDialogView), parameters, async r =>
            {
                if (r.Result == ButtonResult.OK)
                {
                    var data = parameters.GetValue<TimerTask>("param4");

                    if (data != null)
                    {
                        data.DaysRemak = string.Join(",", data.SelectedDays.Select(item => item.Name));

                        var ttData = data.DtotoDb(data);
                        
                        await abstractTimerTaskServer.InsertAsync(ttData);
                        MessageSuccess("保存成功！");
                        await LoadingMaskInterceptor.Instance().InterceptAsync(async () => await Task.Run(() => { Query(); }));
                    }
                }
            });
        }
        private void Query()
        {
            var data =  abstractTimerTaskServer.Query();
            TimerTask timer = new TimerTask();
            Timers = new ObservableCollection<TimerTask>();
            foreach (var item in data)
            {
                Timers.Add(timer.DbtoDto(item));
            }
            TimerEvent.Instance.PushMessage(TimerEventEnum.ScheduledReminders);
        }
        #endregion

        #region 集合
        private ObservableCollection<TimerTask> timers = new ObservableCollection<TimerTask>();

        public ObservableCollection<TimerTask> Timers
        {
            get => timers;
            set
            {
                timers = value;
                RaisePropertyChanged();
            }
        }
        #endregion

        #region 属性
        private TimerTask timerTask;

        public TimerTask TimerTask
        {
            get => timerTask;
            set
            {
                timerTask = value;
                RaisePropertyChanged();
            }
        }
        #endregion

        private string title;

        public string Title { get => title; set => SetProperty(ref title, value); }
    }
}
